#region Copyright RenGuiYou. All rights reserved.

//=====================================================
// PandoraFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================

#endregion

using System.Collections.Generic;
using System.Linq;
using System.Text;
using Pandora;
using UnityEditor;
using UnityEngine;
using Object = UnityEngine.Object;

namespace PandoraEditor
{
    public static class AtlasExtend
    {
        public static void UpdateHierarchyDepend()
        {
            var title = "刷新预制GUID";
            EditorUtility.DisplayProgressBar(title, "Please Wait......", 0.0f);
            PdrWindowHelper.ClearConsole();
            // var list = PdrFindBase.GetPrefabs("Assets/art/spine");
            var list = PdrFindBase.GetPrefabs("Assets/data/ui/panel");
            for (int index = 0; index < list.Count; ++index)
            {
                var assetObject = AssetDatabase.LoadAssetAtPath<GameObject>(list[index]);
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append(assetObject.name).Append("(").Append(index + 1).Append("/").Append(list.Count).Append(")");
                EditorUtility.DisplayProgressBar(title, stringBuilder.ToString(), (float)index * 1f / (float)list.Count);
                var source = assetObject.gameObject;
                GameObject newGo = PrefabUtility.InstantiatePrefab(source.gameObject) as GameObject;
                PrefabUtility.ReplacePrefab(newGo, source.gameObject, ReplacePrefabOptions.ConnectToPrefab);
                Object.DestroyImmediate(newGo);
            }

            EditorUtility.ClearProgressBar();
            AssetDatabase.Refresh();
        }

        public static void AddSpriteInfo(Object atlasInfo, List<Sprite> spriteList)
        {
            NAtlas atlas = atlasInfo as NAtlas;
            atlas.sprites = spriteList.ToArray();
        }

        public static Object GetComponent(GameObject gameObject, Material mat)
        {
            var ai = gameObject.GetComponent<NAtlas>() ?? gameObject.AddComponent<NAtlas>();
            ai.material = mat;
            return ai;
        }

        public static void ClearEmptyAtlas()
        {
            var dirNameArray = PdrFileUtil.GetDirsName(UserSetting.UISetting.path_atlas);
            var fileList = PdrFileUtil.GetFilesName(UserSetting.UISetting.path_tp);
            HashSet<string> atlasNameSet = new HashSet<string>();
            foreach (var filePath in fileList)
            {
                var atlasName = GetFileAtlasName(filePath.GetFileName());
                if (dirNameArray.Contains(atlasName))
                {
                    continue;
                }

                Debug.Log(atlasName);
                atlasNameSet.Add(atlasName);
                PdrFileUtil.DeleteFile(filePath);
            }

            var atlasConfig = AssetDatabase.LoadAssetAtPath<AtlasConfig>(UserSetting.UISetting.path_atlas_asset);

            for (var i = atlasConfig.atlasList.Count - 1; i >= 0; i--)
            {
                var atlasName = GetFileAtlasName(atlasConfig.atlasList[i].name);
                if (!dirNameArray.Contains(atlasName))
                {
                    atlasConfig.atlasList.RemoveAt(i);
                }
            }

            EditorUtility.SetDirty(atlasConfig);
            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
        }

        private static string GetFileAtlasName(string source)
        {
            source = source.Split(".")[0];
            string[] formatArray = { "_c", "_mat", "_1", "_2", "_3", "_4", "_5", "_6", "_7", "_8", "_9", "_10", "_11", "_12", "_13", "_14", "_15", "_16", "_17", "_18", "_19" };
            foreach (var formatStr in formatArray)
            {
                source = source.Replace(formatStr, "");
            }

            return source;
        }
    }
}